package com.skyolder.crutch.ast;

import java.util.ArrayList;

import com.skyolder.crutch.error.GrammarError;
import com.skyolder.crutch.ev.Environment;
import com.skyolder.crutch.word.TokenQueue;
import com.skyolder.crutch.word.token.IdentifierToken;
import com.skyolder.crutch.word.token.KeyWordToken;
import com.skyolder.crutch.word.token.SymbolToken;
import com.skyolder.crutch.word.token.Token;
import com.skyolder.crutch.word.types.KeyWordType;
import com.skyolder.crutch.word.types.SymbolType;

public class FunctionTree implements Tree {
	Environment parentEnvironment = null;
	TokenQueue tokenQueue = null;
	Environment functionEnvironment = null;
	String returnType = null;
	String name = null;
	ArrayList<String> params = new ArrayList<String>();
	ArrayList<StatementTree> statementTrees=new ArrayList<StatementTree>();
	public FunctionTree(Environment environment, TokenQueue tokenQueue) {
		parentEnvironment = environment;
		this.tokenQueue = tokenQueue;
		functionEnvironment = new Environment(parentEnvironment);
	}

	public boolean isMain() {
		return false;
	}

	@Override
	public void build() throws GrammarError {
		// TODO Auto-generated method stub
		Token token = tokenQueue.nextToken();
		if (!(token instanceof KeyWordToken)) {
			throw new GrammarError(token.toString());
		}
		if (((KeyWordToken) token).getKeyWordType() != KeyWordType.FUNCTION) {
			throw new GrammarError(token.toString());
		}
		token = tokenQueue.nextToken();
		if (!(token instanceof KeyWordToken) || !(token instanceof IdentifierToken)) {
			throw new GrammarError(token.toString());
		}
		if (token instanceof KeyWordToken) {
			if (((KeyWordToken) token).getKeyWordType() != KeyWordType.VOID) {
				throw new GrammarError(token.toString());
			}
			returnType = null;
		} else {
			returnType = ((IdentifierToken) token).getIdent();
		}
		token = tokenQueue.nextToken();
		if (!(token instanceof IdentifierToken)) {
			throw new GrammarError(token.toString());
		}
		name = ((IdentifierToken) token).getIdent();
		if (name.contains(".")) {
			throw new GrammarError(name);
		}
		token = tokenQueue.nextToken();
		if (!(token instanceof SymbolToken)) {
			throw new GrammarError(token.toString());
		}
		if (((SymbolToken) token).getSymbolType() != SymbolType.LBRACKET) {
			throw new GrammarError(token.toString());
		}
		for (;;) {
			token = tokenQueue.nextToken();
			if (token instanceof SymbolToken) {
				if (((SymbolToken) token).getSymbolType() == SymbolType.RBRACKET) {
					break;
				}
			}
			if (token instanceof IdentifierToken) {
				if (!((IdentifierToken) token).isSimpleIdentifier()) {
					throw new GrammarError(token.toString());
				}
				params.add(((IdentifierToken) token).getIdent());
			}
			token = tokenQueue.nextToken();
			if (token instanceof SymbolToken) {
				if (((SymbolToken) token).getSymbolType() == SymbolType.RBRACKET) {
					break;
				}
				if(((SymbolToken) token).getSymbolType()!=SymbolType.COMMA){
					throw new GrammarError(token.toString());
				}else{
					continue;
				}
			}
			throw new GrammarError(token.toString());
		}
		
		token = tokenQueue.nextToken();
		if (!(token instanceof SymbolToken)) {
			throw new GrammarError(token.toString());
		}
		if(((SymbolToken)token).getSymbolType()!=SymbolType.LBRACE){
			throw new GrammarError(token.toString());
		}
		for (;;) {
			token = tokenQueue.peekToken();
			if (token instanceof SymbolToken) {
				if (((SymbolToken) token).getSymbolType() == SymbolType.RBRACKET) {
					tokenQueue.nextToken();
					break;
				}
			}
			StatementTree statementTree=new StatementTree(functionEnvironment,tokenQueue);
			statementTree.build();
			this.statementTrees.add(statementTree);
		}
	}

	@Override
	public void excute() {
		// TODO Auto-generated method stub

	}

	public String getName() {
		return null;
	}
}
